package rcs34.android.photopublishare;

import android.content.ContentValues;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class AdapterSelectTarget extends BaseAdapter implements ListAdapter {
	private final String LOGTAG = getClass().getName();
	private final int CHECKBOX_ID_OFFSET=1000;
	private ActivitySelectTarget activity;
	private OnClickListener clickListener;

	public AdapterSelectTarget(ActivitySelectTarget _activity) {
		super();
		activity = _activity;
		clickListener = new View.OnClickListener() {
			public void onClick(View v) {
				int position = v.getId() - CHECKBOX_ID_OFFSET;
				Log.v(LOGTAG, position + "");
				ContentValues item = GApp.getConfiguredTargets().get(position);
				if (item.getAsString(DBHelper.COL_SELECTED).equals("1")) {
					item.put(DBHelper.COL_SELECTED, 0);
				} else {
					item.put(DBHelper.COL_SELECTED, 1);
				}
				notifyDataSetChanged();
			}
		};
		dataChanged();
	}

	private class ViewHolder {
		public TextView name;
		public TextView url;
		public CheckBox checkBox;
	}

	public void dataChanged() {
		Populator populator = new Populator();
		populator.execute(0);
	}

	public View getView(int _position, View _convertView, ViewGroup _parent) {
		View result = _convertView;
		if (result == null) {
			LayoutInflater inflater = activity.getLayoutInflater();
			result = inflater.inflate(R.layout.item_target, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.name = (TextView) result
					.findViewById(R.id.itemTargetName);
			viewHolder.url = (TextView) result.findViewById(R.id.itemTargetUrl);
			viewHolder.checkBox = (CheckBox) result
					.findViewById(R.id.itemTargetCheckBox);
			result.setTag(viewHolder);
		}
		ViewHolder holder = (ViewHolder) result.getTag();
		ContentValues item = GApp.getConfiguredTargets().get(_position);
		holder.name.setText(item.getAsString(DBHelper.COL_NAME));
		holder.url.setText(item.getAsString(DBHelper.COL_URL));
		holder.checkBox.setChecked(item.getAsString(DBHelper.COL_SELECTED)
				.equals("1"));
		holder.checkBox.setId(_position + CHECKBOX_ID_OFFSET);
		holder.checkBox.setOnClickListener(clickListener);
		result.setOnLongClickListener(activity.getLongClickListener());
		return result;
	}

	public int getCount() {
		if (GApp.getConfiguredTargets() != null) {
			return GApp.getConfiguredTargets().size();
		} else {
			return 0;
		}
	}

	public Object getItem(int _position) {
		return GApp.getConfiguredTargets().get(_position);
	}

	public long getItemId(int _position) {
		return _position;
	}

	public int getItemViewType(int _position) {
		return 0;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isEmpty() {
		return (GApp.getConfiguredTargets() == null || GApp.getConfiguredTargets().size() < 1);
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int position) {
		return true;
	}

	private class Populator extends AsyncTask<Integer, Integer, Integer> {
		@Override
		protected Integer doInBackground(Integer... _void) {
			DBHelper dbHelper = new DBHelper();
			GApp.setConfiguredTargets(dbHelper.getAllTargets());
			return 0;
		}

		@Override
		protected void onPostExecute(Integer result) {
			notifyDataSetChanged();
		}
	}
}
